package cn.fl.institution.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import cn.fl.project.service.IPrjCstAccInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 项目基础信息表 控制层 平安银行
 */
@Controller
@RequestMapping("/institution/pabank")
public class PrjBscInfoInsPabankController {

    @Autowired
    private IPrjCstAccInfoService prjCstAccInfoService;//项目客户基础信息表

    @Autowired
    private ICstNpBscInfoService cstNpBscInfoService;

    /**
     * 保存 项目客户信息
     */
    @RequestMapping(value = "/saveForPabank.do", method = RequestMethod.POST)
    @ResponseBody
    public DataResultVO saveForPabank(HttpServletRequest request) throws BaseException {
        BaseLogUtils.newLogger("saveForPabank.修改项目客户账户信息："+ JSON.toJSONString(request.getParameterMap()) + "，操作人：" + CurrentThreadContext.getCurrentUserName()).info();
        String cstAccInfoId = request.getParameter("cstAccInfoId");//项目客户信息表id
        String insCode = request.getParameter("insCode");//机构code
        String prjId = request.getParameter("prjId");//项目id
        String cstId = request.getParameter("cstId");//客户id
        String cstNm = request.getParameter("cstNm");//客户姓名
        String certNo = request.getParameter("certNo");//身份证号
        String accNo = request.getParameter("accNo");
        String mp = request.getParameter("mp");
        String bkCd = request.getParameter("bkCd");
        String bkNm = request.getParameter("bkNm");

        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存成功");
        try {
            if (StringUtils.isEmpty(prjId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "项目id为空!");
            }
            if (StringUtils.isEmpty(cstId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户id为空!");
            }
            if (StringUtils.isEmpty(cstNm)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户名称为空!");
            }
            if (StringUtils.isEmpty(certNo)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户证件号为空!");
            }
            if (StringUtils.isEmpty(bkNm) || StringUtils.isEmpty(bkCd)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "开户银行为空!");
            }
            if (StringUtils.isEmpty(accNo)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "银行卡号为空!");
            }
            if (StringUtils.isEmpty(mp)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "银行预留手机号为空!");
            }

            // 修改项目客户信息
            PrjCstAccInfoQueryCondition condition = new PrjCstAccInfoQueryCondition();
            if (StringUtils.isNotEmpty(cstAccInfoId)) {
                condition.setId(Long.valueOf(cstAccInfoId));
            }
            condition.setBkCd(bkCd);
            condition.setBkNm(bkNm);
            condition.setPrjId(Long.valueOf(prjId));
            condition.setCstId(Long.valueOf(cstId));
            condition.setInsCode(insCode);
            condition.setCstNm(cstNm);
            condition.setAccNo(accNo.trim());
            condition.setAccNm(cstNm);
            condition.setMp(mp);
            condition.setCertNo(certNo.trim());
            condition.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);
            BaseLogUtils.newLogger("saveForPabank.修改项目客户账户信息,condition:" + JSONObject.toJSONString(condition)).info();
            dataResultVO = this.prjCstAccInfoService.modifyPrjAccInfo(condition);
            if (BaseConstant.IS_YESNO_YES == dataResultVO.getSuccess()) {
                Map<String,String> resultMap = (Map<String, String>) dataResultVO.getData();
                String chkRstCd = "";
                String rstDesc = "";
                String bankCardType = "";
                chkRstCd = resultMap.get("chkRstCd");
                rstDesc = resultMap.get("rstDesc");
                bankCardType = resultMap.get("bankCardType");
                //四要素结果
                if (!BaseConstant.IS_YESNO_YES_STR.equals(chkRstCd)) {
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    dataResultVO.setInfo(rstDesc);
                    return dataResultVO;
                }
                //银行卡类型
                if (!BaseConstant.IS_YESNO_NO_STR.equals(bankCardType)) {
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    dataResultVO.setInfo("不支持此类银行卡，请更换为借记卡");
                    return dataResultVO;
                }
            }
        } catch (BaseException ex) {
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo(ex.getMessage());
            BaseLogUtils.newLogger("saveForPabank.保存项目客户账户信息及校验,prjId:" + prjId + ".ERROR:" + ex.getMessage()).info();
        } catch (Exception e) {
            BaseLogUtils.newLogger("saveForPabank.保存失败，系统异常" + e).info();
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "保存失败，系统异常");
        }
        BaseLogUtils.newLogger("saveForPabank.保存项目客户账户信息及校验返回dataResultVO=" + JSON.toJSONString(dataResultVO)).info();
        return dataResultVO;
    }
}
